using System;
using System.Collections.Generic;
using System.Text;
using GarageGames.Torque.Materials;
using Microsoft.Xna.Framework.Graphics;
using Xyne.Objects;
using GarageGames.Torque.Core;

namespace Xyne.Templates.PuzzlePieceTemplate
{
    public class PuzzlePieceMaterialManager
    {
        public static PuzzlePieceMaterialManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new PuzzlePieceMaterialManager();
                }

                return instance;
            }
        }

        public PuzzlePieceMaterialManager()
        {
            puzzlePieceMaterials = new LightingMaterial[(int)PuzzlePieceColorEnum.Length];
            puzzlePieceTextures = new Texture2D[(int)PuzzlePieceColorEnum.Length];
            random = new Random(DateTime.Now.Millisecond);
        }

        public LightingMaterial GetPuzzlePieceMaterial(PuzzlePieceColorEnum color)
        {
            switch (color)
            {
                case PuzzlePieceColorEnum.Red:
                    return (LightingMaterial)TorqueObjectDatabase.Instance.FindObject<LightingMaterial>("RedLightMaterial").Clone();
                case PuzzlePieceColorEnum.Blue:
                    return (LightingMaterial)TorqueObjectDatabase.Instance.FindObject<LightingMaterial>("BlueLightMaterial").Clone();
                case PuzzlePieceColorEnum.Green:
                    return (LightingMaterial)TorqueObjectDatabase.Instance.FindObject<LightingMaterial>("GreenLightMaterial").Clone();
                case PuzzlePieceColorEnum.Yellow:
                    return (LightingMaterial)TorqueObjectDatabase.Instance.FindObject<LightingMaterial>("YellowLightMaterial").Clone();
                case PuzzlePieceColorEnum.White:
                    return (LightingMaterial)TorqueObjectDatabase.Instance.FindObject<LightingMaterial>("WhiteLightMaterial").Clone();
                case PuzzlePieceColorEnum.Black:
                    return (LightingMaterial)TorqueObjectDatabase.Instance.FindObject<LightingMaterial>("BlackLightMaterial").Clone();
            }

            return null;
        }

        public Texture2D GetPuzzlePieceTexture(PuzzlePieceColorEnum color)
        {
            if (puzzlePieceTextures[(int)color] == null)
            {
                switch (color)
                {
                    case PuzzlePieceColorEnum.Black:
                        puzzlePieceTextures[(int)color] = Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\puzzlepiece\puzzlepieceblack");
                        break;
                    case PuzzlePieceColorEnum.Blue:
                        puzzlePieceTextures[(int)color] = Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\puzzlepiece\puzzlepieceblue");
                        break;
                    case PuzzlePieceColorEnum.Green:
                        puzzlePieceTextures[(int)color] = Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\puzzlepiece\puzzlepiecegreen");
                        break;
                    case PuzzlePieceColorEnum.Red:
                        puzzlePieceTextures[(int)color] = Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\puzzlepiece\puzzlepiecered");
                        break;
                    case PuzzlePieceColorEnum.White:
                        puzzlePieceTextures[(int)color] = Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\puzzlepiece\puzzlepiecewhite");
                        break;
                    case PuzzlePieceColorEnum.Yellow:
                        puzzlePieceTextures[(int)color] = Game.Instance.Content.Load<Texture2D>(@".\data\images\ui\puzzlepiece\puzzlepieceyellow");
                        break;
                }
            }
            return puzzlePieceTextures[(int)color];
        }

        public PuzzlePieceColorEnum GetRandomColor()
        {
            return (PuzzlePieceColorEnum)random.Next((int)PuzzlePieceColorEnum.Length);
        }

        #region Static Variables
        private static PuzzlePieceMaterialManager instance;
        #endregion

        #region Members
        private LightingMaterial[] puzzlePieceMaterials;
        private Texture2D[] puzzlePieceTextures;
        private Random random;
        #endregion
    }
}
